package DBLayer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import ModelLayer.Club;



public class DBClub {
	
private Connection con;
	
	public DBClub(){
		con = DbConnection1.getInstance().getDBcon();
		}
	
	
	public Club findClub(int club_id, boolean retrieveAssociation) {
		String wClause = " club_id = '" + club_id + "'";
		return singleWhere(wClause, retrieveAssociation);
		}
	
	private Club singleWhere(String wClause, boolean retrieveAssociation)
	  {
	   ResultSet results;
	   Club clubObj = new Club();
	   clubObj.setClubId(0);
	   	String query =  buildQuery(wClause);
	                 System.out.println(query);
	   try{ // read the employee from the database
	     Statement stmt = con.createStatement();
	     stmt.setQueryTimeout(5);
	     results = stmt.executeQuery(query);
	     
	     if( results.next() ){
	                             clubObj = buildClub(results);
	                             //association is to be build
	                            
	                             stmt.close();
	     			}
	                         else{ //no customer was found
	                             clubObj = null;
	                         }
	   }//end try 
	    catch(Exception e){
	     System.out.println("Query exception: "+e);
	    }
	   return clubObj;
	  }
	
	 private String buildQuery(String wClause)
	 {
	     String query="SELECT club_id, name FROM club";
	  
	  if (wClause.length()>0)
	   query=query+" WHERE "+ wClause;
	   
	  return query;
	 }
	 
	 private Club buildClub(ResultSet results)
     {   Club clubObj = new Club();
         try{ // the columns from the table postal  are used
        	   clubObj.setClubId(results.getInt("club_id"));
               clubObj.setName(results.getString("name"));
                }
        catch(Exception e)
        {
            System.out.println("error in building the club object");
        }
        return clubObj;
     }

}//end class


